package com.example.smartcitydemo.network.client

import com.example.smartcitydemo.IP
import com.example.smartcitydemo.MyApplication
import com.example.smartcitydemo.network.api.NoAuthService
import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory
import kotlinx.serialization.json.Json
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import retrofit2.Retrofit

/**
 * @PACKAGE_NAME: com.example.smartcitydemo.network.client
 * @NAME: Caleb.F
 * @DATE: 2021/11/3 8:19
 * @Email: 1919796283@qq.com
 */
val noAuthApiService: NoAuthService by lazy{
    NoAuthClient.getApi(
        NoAuthService::class.java,
        MyApplication.sp.getString(IP,"http://124.93.196.45:10001")!!
    )
}

object NoAuthClient : BaseRetrofit() {
    //没有token的什么都不要加
    override fun setHttpClientBuilder(builder: OkHttpClient.Builder): OkHttpClient.Builder {
        return builder
    }

    //个性化设置: 配置解析器
    override fun setRetrofitBuilder(retrofitBuilder: Retrofit.Builder): Retrofit.Builder {
        val contentType = "application/json".toMediaType()
        return retrofitBuilder.addConverterFactory(Json {
            //忽略缺少的键值
            ignoreUnknownKeys = true
            //允许默认值参数
            encodeDefaults = true
            //允许null值,替换为默认值
            coerceInputValues = true
        }.asConverterFactory(contentType))
    }

}